<?php
/**
 * Created by PhpStorm.
 * User: yyq
 * Date: 2017/12/22
 * Time: 11:02
 */

namespace Information\Controller;


use Common\Controller\AdminbaseController;
use Information\Model\InformationModel;

class PartnerController extends AdminbaseController
{
    private $information_model;

    public function __construct()
    {
        parent::__construct();
        $this->information_model = new InformationModel();
    }

    public function lists() {
        //搜索条件
        $where = array();
        if(IS_POST) {
            $title = I('post.title');
            if($title) {
                $where['title'] = array('like', '%'.$title.'%');
            }

//            $inf_type = I('post.inf_type');
            /*if($type) {
                $where['inf_type'] = $type;
            }*/
        } else {
            $title = I('get.title');
            if($title) {
                $where['title'] = array('like', '%'.$title.'%');
            }
            /*$inf_type = I('get.inf_type');
            if($inf_type) {
                $where['inf_type'] = $inf_type;
            }*/
        }
        $where['inf_type'] = '12';
        //类型

        $count = $this->information_model
            ->where($where)
            ->count();
        $page = $this->page($count, C('PAGE_NUMBER'));
        $infomations = $this->information_model
            ->where($where)
            ->field('title, create_time, id, cover, inf_type,recommend')
            ->limit($page->firstRow . ',' . $page->listRows)
            ->order('id desc')
            ->select();

        foreach($infomations as $k => $v) {
            $result[$k]['str_manage'] .= '<a class="js-ajax-btn-dialog" href="' . U('Information/edit', ['id' => $v['id']]) . '">编辑</a> | <a class="js-ajax-btn-dialog js-ajax-delete" href="' . U('Information/delete', ['id' => $v['id']]) . '">删除</a>';
            $strVipFlag=($v['recommend']>0)?"<span style='color:red;'>推荐</span>":"非推荐";
            $lists .= '<tr>
               <td>' . ($k + 1) . '</td>
               <td>' . $strVipFlag . '</td>
               <td>' . '<a  class="js-ajax-btn-dialog" href="' . U('Information/detail', ['id' => $v['id']]) . '">'.$v['title'].'</a>' . '</td>
               <td><img src="' . getUrl($v['cover']) . '"></td>
               <td>'.$v['create_time'] . '</td>
               <td>' . $result[$k]['str_manage'] . '</td>
           </tr>';

        }

        $this->assign('options', $options);
        $this->assign('lists', $lists);
        $this->assign('formget', I(''));
        $this->assign('Page',$page->show());
        $this->display();
    }

    public function add() {

        if(IS_POST) {
            $image = upload_local('Information');
            /*if(!$image) {
                $this->error('封面不能为空');
            }*/
            /*if (!empty($_POST['photos_alt']) && !empty($_POST['photos_url'])) {
                foreach ($_POST['photos_url'] as $key => $url) {
                    $photourl = sp_asset_relative_url($url);
                    $_POST['post']['smeta'][] = ["url" => $photourl, "alt" => $_POST['photos_alt'][$key]];
                }
            }*/


            $post = I('post.post');//dump($post);exit;
//            $data['smeta'] = json_encode($_POST['post']['smeta']);
            $data['discribe'] = $post['discribe'];
            $data['content'] = htmlspecialchars_decode($post['content']);
            $data['title'] = $post['title'];
            $data['create_time'] = date('Y-m-d H:i:s');
            $data['cover'] = $image[0];
            $data['inf_type'] = $post['inf_type'];
            $data["recommend"]=$post["recommend"];
//            var_dump($_POST);
//            var_dump($data);exit;
            if($this->information_model->add($data) == false) {
                $this->error();
            } else {
                $this->success();
            }

        } else {
            $this->display();
        }
    }
}